package com.bingyuan.aipassproject.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.bingyuan.aipassproject.R;
import com.bingyuan.aipassproject.entity.ProgressTestBean;
import com.bingyuan.aipassproject.utils.DpUtils;

import java.util.ArrayList;
import java.util.List;

public class ProgressbarView extends View {

    private final int DEFAULT_WIDTH = 450;
    private final int DEFAULT_HEIGTH = 40;
    private final int DEFAULT_SHAPE = 0;
    private final int DEFAULT_MAX = 100;
    private final int DEFAULT_PROGRESS = 0;
    private final int DEFAULT_PROGRESS_COLOR = Color.parseColor("#FFD413");
    private final int DEFAULT_PROGRESS_BACK_COLOR = Color.parseColor("#191923");
    private final int DEFAULT_CIRCLE_R = 50;
    private final int DEFAULT_SQUARE_R = 20;
    private final int DEFAULT_TEXT_SIZE = 30;

    private enum ShapeType {
        RECTANGLE,
        CIRCLE,
        SQUARE
    }

    private int progress = DEFAULT_PROGRESS;
    private int max = DEFAULT_MAX;
    private float mwidth = DEFAULT_WIDTH;
    private float mhight = DEFAULT_HEIGTH;
    private int mShapeType = DEFAULT_SHAPE;
    private int proColor = DEFAULT_PROGRESS_COLOR;
    private int proBackColor = DEFAULT_PROGRESS_BACK_COLOR;
    private int textLowColor = Color.WHITE;
    private int textMiddleColor = Color.BLUE;
    private int textHighColor = Color.RED;
    private int progressSegmentColor = proColor;
    private int progressDoubleSegColor = Color.GRAY;

    private float squareRadius = DEFAULT_SQUARE_R;
    private float textSize = DEFAULT_TEXT_SIZE;

    private boolean showText;
    private float circleX = DEFAULT_CIRCLE_R + 20;
    private float circleR = DEFAULT_CIRCLE_R;

    private float startX;
    private float startY;
    private float startR = -90;
    private Paint paint;
    RectF circleRectf, squareRectf;
    private List<Integer> progressList;
    private Context context;

    //新增
    private ArrayList<ProgressTestBean> mProgressList;
    private final String[] mListColor = {"#E83A22", "#FA6101", "#FF8E16", "#FFAB15", "#FFD413"};
    private final String[] mListText = {"Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ"};


    public ProgressbarView(Context context) {
        this(context, null);
    }

    public ProgressbarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressbarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    public void setmProgressList(ArrayList<ProgressTestBean> mProgressList) {
        this.mProgressList = mProgressList;
        invalidate();
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        // Load the styled attri
        // butes and set their properties
        this.context = context;
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ProgressbarView, defStyleAttr, 0);
        mwidth = attributes.getDimension(R.styleable.ProgressbarView_p_width, DEFAULT_WIDTH);
        mhight = attributes.getDimension(R.styleable.ProgressbarView_p_height, DEFAULT_HEIGTH);
        mShapeType = attributes.getInteger(R.styleable.ProgressbarView_p_shapeType, DEFAULT_SHAPE);
        showText = attributes.getBoolean(R.styleable.ProgressbarView_p_showText, false);
        if (mShapeType == 1) {
            circleR = attributes.getDimension(R.styleable.ProgressbarView_p_circle_radius, DEFAULT_CIRCLE_R);
//            showText = true;
            circleX = attributes.getDimension(R.styleable.ProgressbarView_p_circle_X_Y, circleR + 20);
        }
        if (mShapeType == 2) {
            squareRadius = attributes.getDimension(R.styleable.ProgressbarView_p_square_radius, DEFAULT_SQUARE_R);
        }

        if (showText) {
            textSize = attributes.getDimensionPixelSize(R.styleable.ProgressbarView_p_textSize, DEFAULT_TEXT_SIZE);
        }
        max = attributes.getInteger(R.styleable.ProgressbarView_p_maxValue, DEFAULT_MAX);
        progress = attributes.getInteger(R.styleable.ProgressbarView_p_progressValue, DEFAULT_PROGRESS);
        proColor = attributes.getColor(R.styleable.ProgressbarView_p_progressColor, DEFAULT_PROGRESS_COLOR);
        proBackColor = attributes.getColor(R.styleable.ProgressbarView_p_progressBackColor, DEFAULT_PROGRESS_BACK_COLOR);
        textLowColor = attributes.getColor(R.styleable.ProgressbarView_p_textLowColor, Color.BLACK);
        textMiddleColor = attributes.getColor(R.styleable.ProgressbarView_p_textMiddleColor, Color.BLUE);
        textHighColor = attributes.getColor(R.styleable.ProgressbarView_p_textHighColor, Color.RED);
        progressSegmentColor = attributes.getColor(R.styleable.ProgressbarView_p_progressSegmentColor, DEFAULT_PROGRESS_COLOR);
        progressDoubleSegColor = attributes.getColor(R.styleable.ProgressbarView_p_progressDoubleSegColor, Color.GRAY);

        Log.i("slack", mwidth + "," + mhight + "," + mShapeType + "," + max + "," +
                progress + "," + showText + "," + squareRadius + "," + textSize);
        paint = new Paint();
        circleRectf = new RectF();
        squareRectf = new RectF();
        progressList = new ArrayList<>();
        mProgressList = new ArrayList<>();
        paint.setAntiAlias(true);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#EFEFEF"));
        paint.setStrokeWidth(10);
//        paint.setStyle(Paint.Style.STROKE);
        paint.setStyle(Paint.Style.FILL);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(proBackColor);
        paint.setStrokeWidth(10);
        paint.setTextSize(textSize);
        startX = 0;
        startY = 0;

        switch (mShapeType) {
            case 0:
                canvas.drawRect(startX, startY, mwidth, mhight, paint);
                paint.setColor(proColor);
                if (progressList.size() > 0) {
                    for (int pro : progressList) {
                        canvas.drawRect(startX, startY, ((float) pro / max) * mwidth,
                                mhight, paint);
                        startX = ((float) pro / max) * mwidth;
//                        Log.i("slack","startX:" + startX + "," + pro + "," + max +"," + mwidth);
                        paint.setColor(progressSegmentColor);
                    }
                } else {
                    canvas.drawRect(startX, startY, ((float) progress / max) * mwidth,
                            mhight, paint);
                }


//                paint.setColor(Color.RED);
//                canvas.drawLine(startX, startY + (mhight - startY) / 4 * 2,
//                        ((float) progress / max) * mwidth, startY + (mhight - startY)
//                                / 4 * 2, paint);

                paint.setStrokeWidth(1);//
                paint.setColor(progressDoubleSegColor);

                if (progressList.size() > 0) {
                    for (int i = 0; i < progressList.size() - 1; i++) {
                        canvas.drawLine(((float) progressList.get(i) / max) * mwidth, startY, ((float) progressList.get(i) / max) * mwidth, startY + (mhight - startY), paint);
                    }
                }
                if (showText) {

                    if (progress < (max / 3)) {
                        paint.setColor(textLowColor);
                    } else if (progress < (max / 3) * 2 && progress > (max / 3)) {
                        paint.setColor(textMiddleColor);
                    } else {
                        paint.setColor(textHighColor);
                    }

                    canvas.drawText(progress + "%", ((float) progress / max) * mwidth - (progress == 0 ? 0 : textSize),
                            mhight + textSize, paint);
                }
                break;
            case 1:
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(proColor);
                circleRectf.set(circleX, circleX, circleX + circleR * 2, circleX + circleR * 2);
                if (progressList.size() > 0) {
                    startR = -90;
                    float last = 0;
                    boolean isOu = true;
                    for (int pro : progressList) {
                        canvas.drawArc(circleRectf, startR, ((pro - last) / max) * 360, false, paint);
                        startR = ((pro - last) / max) * 360 + startR;
                        last = pro;
//                        Log.i("slack","startR:" + startR);
                        if (isOu) {
                            paint.setColor(progressSegmentColor);
                        } else {
                            paint.setColor(proColor);
                        }
                        isOu = !isOu;
                    }
                } else {
                    canvas.drawArc(circleRectf, startR, ((float) progress / max) * 360, false, paint);
                }


                if (showText) {
                    paint.reset();
                    paint.setStrokeWidth(1);
                    paint.setTextSize(textSize);
                    if (progress < (max / 3)) {
                        paint.setColor(textLowColor);
                    } else if (progress < (max / 3) * 2 && progress > (max / 3)) {
                        paint.setColor(textMiddleColor);
                    } else {
                        paint.setColor(textHighColor);
                    }
                    if (progress == max) {
                        canvas.drawText("done", circleX + textSize / 2f, circleX + textSize * 2f, paint);
                    } else {
                        canvas.drawText(progress + "%", circleX + textSize / 2f, circleX + textSize * 2f, paint);
                    }
                }
                break;
            case 2:
                //先绘制背景
                paint.setColor(proBackColor);
                squareRectf.set(startX, startY + DpUtils.dip2px(context, 5) + textSize, mwidth, mhight);
                canvas.drawRoundRect(squareRectf, squareRadius, squareRadius, paint);
                for (int i = mProgressList.size(); i > 0; i--) {
                    int progress = 0;
                    //计算当前颜色总进度
                    for (int j = 0; j < i; j++) {
                        progress += mProgressList.get(j).getProgress();
                    }
                    int color = Color.parseColor(mListColor[i - 1]);
                    paint.setColor(color);
                    squareRectf.set(startX, startY + DpUtils.dip2px(context, 5) + textSize, ((float) progress / max) * mwidth, mhight + DpUtils.dip2px(context, 5) + textSize);
                    canvas.drawRoundRect(squareRectf, squareRadius, squareRadius, paint);
                    //设置分割线,如果为最后一个则不设置分割线
                    if (i != mProgressList.size()) {
                        paint.setColor(proBackColor);
                        paint.setStrokeWidth(DpUtils.dip2px(context, 4));
                        canvas.drawLine(((float) progress / max) * mwidth, textSize + DpUtils.dip2px(context, 5), ((float) progress / max) * mwidth, startY + (mhight - startY) + textSize + DpUtils.dip2px(context, 5), paint);
                    }
                    //绘制字体
                    if (showText) {
                        if (i == mProgressList.size()) {
                            paint.setColor(textHighColor);
                        } else {
                            paint.setColor(textLowColor);
                        }
                        int textProgress = 0;
                        //计算当前颜色总进度
                        for (int k = 0; k < i - 1; k++) {
                            textProgress += mProgressList.get(k).getMaxProgress();
                        }
                        canvas.drawText(mListText[i - 1], ((float) textProgress / max) * mwidth + ((float) mProgressList.get(i - 1).getMaxProgress() / max * mwidth) / 2 - textSize / 2,
                                mhight + 2 * textSize + 2 * (DpUtils.dip2px(context, 5)), paint);
                        //绘制顶部总数
                        if (i == mProgressList.size()) {
                            //绘制顶部总数
                            canvas.drawText(mProgressList.get(i - 1).getNum(), ((float) progress / max) * mwidth,
                                    textSize, paint);
                        }
                    }
                }

//        if (showText) {
//            if (progress < (max / 3)) {
//                paint.setColor(textLowColor);
//            } else if (progress < (max / 3) * 2 && progress > (max / 3)) {
//                paint.setColor(textMiddleColor);
//            } else {
//                paint.setColor(textHighColor);
//            }
//            canvas.drawText(mListText[], ((float) progress / max) * mwidth - (progress == 0 ? 0 : textSize),
//                    mhight + textSize, paint);
//        }

                break;
            default:
                break;
        }


    }


    // - - - - - - - - - - - - -  public - - - - - - - - - - - - - - - - -


    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        setProgress(progress, false);
    }

    public void setProgress(int progress, boolean needDel) {
        if (progress <= max) {
            this.progress = progress;
            if (needDel) {
                progressList.add(progress);
            }
            invalidate();
        }
    }

    public void removeProgress(int progres) {
        if (progressList.size() > 0) {
            progressList.remove(progressList.size() - 1);
            if (progressList.size() == 0) {
                progress = DEFAULT_PROGRESS;
            } else {
                progress = progressList.get(progressList.size() - 1);
            }
        } else {
            progress = progres;
        }
        invalidate();
    }

}
